<template>
  <div class="detail">
    <van-nav-bar
      title="详情"
      left-text="返回"
      right-text="..."
      left-arrow
      @click-left="onClickLeft"
    />
    <!-- 店铺 信息 -->
    <!-- shopinfo.info && shopinfo.info.poi_back_pic_url 这是为了防止组件初次渲染时shopinfo是空对象,导致的报错. -->
    <div class="shop-head" :style="{ backgroundImage: `url(${ shopinfo.info && shopinfo.info.poi_back_pic_url})`}">
      <img class="logo" v-lazy="shopinfo.info && shopinfo.info.picUrl">
      <span class="name">{{shopinfo.info && shopinfo.info.name}}</span>
      <van-icon @click="collect" name="like-o" color="#ffffff" size="20" />
    </div>
    <!-- 分类及商品列表 -->
    <div class="box">
      <div class="left">
        <!-- 分类列表 -->
        <div @click="currentIndex = index" class="cate" v-for="(item,index) in shopinfo.taglist" :key="index">{{item.name}}</div>
      </div>
      <div class="right">
        <!-- 商品列表 -->
        <div class="good" v-for="(item,index) in shopinfo.taglist && shopinfo.taglist[currentIndex].goodlist" :key="index">
          <div class="img-box">
            <img v-lazy="item.picture">
          </div>
          <div class="text">
            <div class="name">{{item.name}}</div>
            <div class="price">{{item.min_price}}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

import {shop_shopinfo} from '../api'
import { Dialog } from 'vant';
import { Toast } from 'vant';

export default {
  data(){
    return {
      currentIndex:0,//用来保存分类下标
      shopinfo:{}
    }
  },
  methods:{
    onClickLeft(){
      this.$router.go(-1);
    },
    collect(){
      Toast.success('收藏成功');
    }
  },
  mounted(){
    Toast.loading({ duration:0 }) //显示loading
    shop_shopinfo({id: this.$route.params.id }).then((res)=>{
      Toast.clear();//关闭loading提示
      if( res.data.code == 200 ){
        console.log(res.data.shopinfo);
        this.shopinfo = res.data.shopinfo;
      }else{
        //alert(res.data.msg);
        Dialog({ message: res.data.msg }).then((res)=>{
          if( res == 'confirm' ){
            this.$router.go(-1);
          }
        });
      }
    })
  }
}
</script>

<style scoped>
.shop-head{
  background-size: cover;
  display: flex;
  align-items: center;
  padding: 50px 20px;
}
.shop-head img{
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
}
.shop-head .name{
  color: white;
  font-size: 18px;
}
.box {
  display: flex;
}
.box .left{
  width: 120px;
  line-height: 40px;
}
.box .right .good{
  display: flex;
}
.box .right .img-box{
  width: 100px;
  margin-right: 10px;
}
.box .right .img-box img{
  width: 100%;
}
.box .right .text{
  flex:1;
}
</style>